<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * CodeIgniter Auth Class
 *
 * This is Auth library for Codeigniter
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          DVC
 */
class Smauth
{

    public function __construct()
    {
        $this->ci = & get_instance();
    }

    public function authenticate($username, $password, $user)
    {
        if (!empty($user) && $user->password == md5($password)) {
            $this->ci->session->set_userdata('loggedin', true);
            $userProfile = array(
                'id' => $user->userid,
                'username' => $username,
                'firstname' => $user->first_name,
                'lastname' => $user->last_name,
                'email' => $user->email);
            $this->ci->session->set_userdata('role_name', $user->role_name);
            $this->ci->session->set_userdata('role_rule', json_decode($user->role_rule, true));
            $this->ci->session->set_userdata('profile', $userProfile);
            $this->ci->session->set_userdata('userid', $user->userid);
            return true;
        } else {
            return false;
        }
    }

    public function getProfile()
    {
        return $this->ci->session->userdata('profile');
    }

    public function getUserId()
    {
        return $this->ci->session->userdata('userid');
    }

    public function getUserRole()
    {
        return $this->ci->session->userdata('role_name');
    }

    public function isLogin()
    {
        $profile = $this->getProfile();
        if (!empty($profile))
            return true;
        else
            return false;
    }

    public function logout()
    {
        $this->ci->session->sess_destroy();
    }

}
